package com.ashapo.jdbc.l009null;

import java.lang.instrument.IllegalClassFormatException;
import java.sql.*;

public class TestNull {

    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/gm_test";

    //  Database credentials
    static final String USER = "root";
    static final String PASS = "123456";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try{
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();
            String sql = "SELECT id, `name`, sex, add_time, `can_null` FROM `user` WHERE `id`=960";
            ResultSet rs = stmt.executeQuery(sql);

            if(rs.wasNull()){
                System.out.print("RS WAS NULL");
            }

            System.out.println("RS COUNT" + rs.getRow());

            while(rs.next()){
                //Retrieve by column name
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                int sex = rs.getInt("sex");
                int add_time = rs.getInt("add_time");
                String can_null = rs.getString("can_null");
                //int id1  = rs.getInt("id1");

                //Display values
                System.out.print("ID: " + id);
                System.out.print(", Sex: " + sex);
                System.out.print(", Name: " + name);
                System.out.println(", Add_time: " + add_time);
                System.out.println(", can_null: " + can_null);
                //System.out.println(", id1: " + id1);
            }
            rs.close();
        }catch(SQLException se){
            //Handle errors for JDBC
            se.printStackTrace();
        }catch(Exception e){
            //Handle errors for Class.forName
            e.printStackTrace();
        }finally{
            //finally block used to close resources
            try{
                if(stmt!=null)
                    conn.close();
            }catch(SQLException se){
            }// do nothing
            try{
                if(conn!=null)
                    conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }//end finally try
        }//end try
        System.out.println("Goodbye!");
    }
}
